package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    /**
     * 分页查询项目列表
     * @param name
     * @param status
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageResponse<NursingProjectVo> getList(String name, Integer status, Integer page, Integer pageSize) {
        //参数校验
        if (page == null || pageSize == null) {
            throw new BaseException(BasicEnum.PARAM_ERROR);
        }
        PageHelper.startPage(page,pageSize);

        Page<NursingProject> projects = nursingProjectMapper.selectList(name,status);
        PageResponse<NursingProjectVo> nursingProjectVo = PageResponse.of(projects,NursingProjectVo.class);
        return nursingProjectVo;
    }

    /**
     * 新增护理项目
     * @param projectDto
     */
    @Override
    public void addProjict(NursingProjectDto projectDto) {
        //参数校验
        if (projectDto == null || projectDto.getName().isEmpty() || projectDto.getImage().isEmpty() ||
        projectDto.getPrice() == null || projectDto.getOrderNo() == null) {
            throw new BaseException(BasicEnum.PARAM_ERROR);
        }
        NursingProject nursingProject = BeanUtil.toBean(projectDto, NursingProject.class);
        nursingProjectMapper.addProjict(nursingProject);
    }

    @Override
    public NursingProjectVo getById(Long id) {
        if (id == null) {
            throw new BaseException(BasicEnum.PARAM_ERROR);
        }
        //查询项目信息
        NursingProject project = nursingProjectMapper.selectById(id);
        NursingProjectVo projectVo = BeanUtil.toBean(project, NursingProjectVo.class);
        return projectVo;
    }

    /**
     * 更新项目信息
     * @param projectDto
     */
    @Override
    public void update(NursingProjectDto projectDto) {
        if (projectDto == null) {
            throw new BaseException(BasicEnum.PARAM_ERROR);
        }
        NursingProject project = BeanUtil.toBean(projectDto, NursingProject.class);
        nursingProjectMapper.update(project);

    }

    /**
     * 删除护理项目
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        //校验参数
        if (id == null) {
            throw new BaseException(BasicEnum.PARAM_ERROR);
        }
        //删除护理项目
        nursingProjectMapper.deleteById(id);
    }

    /**
     * 更新停用启用
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        //校验参数
        if (id == null || status == null) {
            throw new BaseException(BasicEnum.PARAM_ERROR);
        }
        nursingProjectMapper.updateStatus(id,status);
    }

    /**
     * 查询所有护理项目
     * @return
     */
    @Override
    public List<NursingProjectVo> getAll() {
        List<NursingProjectVo> projectVos = nursingProjectMapper.getAll();
        return projectVos;
    }
}
